<?php
declare (strict_types=1);

namespace app\api\validate;

use app\common\model\User as UserModel;
use think\Validate;

class User extends Validate
{
    protected $rule = [
        "mobile" => "mobile|checkOnly:mobile",
        "nickname" => "chsAlphaNum"
    ];

    protected $message = [
        'mobile.mobile' => '手机号格式有误',
        'mobile.checkOnly' => '手机号已存在',
        'nickname.chsAlphaNum' => '昵称只能是汉字/字母/数字'
    ];

    protected function checkOnly($value, $rule, $data = []): bool
    {
        $that = UserModel::where([$rule => $value]);
        if (!empty($data['id'])) {
            $that->where('id', '<>', $data['id']);
        }
        $count = $that->count();

        return $count ? false : true;
    }
}